@use 'variables' as vars;

@use 'hightlight';

/*
 * RSuite Markdown Styles
 * Organized styling for markdown content in the RSuite documentation
 */

.rcv-markdown {
  // Variables
  --rs-color-muted: #818b981f;

  /* ===== Typography ===== */

  // Headings
  h1 {
    font-size: 28px;

    code {
      font-size: 14px;
      border-radius: 4px;
    }
  }

  h2 {
    font-size: 24px;
    font-weight: bold;
    margin-top: calc(var(--rs-spacing) * 8);
  }

  h3 {
    font-size: 18px;
    margin-top: calc(var(--rs-spacing) * 6);

    code {
      font-size: 18px !important;
    }
  }

  h4 {
    font-size: 16px;
    margin-top: calc(var(--rs-spacing) * 4);
  }

  // Page heading styles
  .page-heading {
    margin-bottom: calc(var(--rs-spacing) * 4);
    line-height: 1.2;
    display: flex;
    align-items: center;
    gap: 6px;

    &[id*='code'] {
      margin-top: calc(var(--rs-spacing) * 6);
    }
  }

  .page-heading-text {
    display: inline-flex;
    align-items: center;

    svg {
      color: rgb(236, 80, 96);
    }
  }

  /* ===== Lists ===== */
  ol,
  ul {
    padding: 10px 20px;
    list-style-type: circle;

    li {
      line-height: 26px;
    }
  }

  /* ===== Tables ===== */
  table {
    width: 100%;
    margin-top: calc(var(--rs-spacing) * 4);

    td,
    th {
      padding: 10px;
      border-style: solid;
      border-width: 1px;
      border-color: var(--rs-border-primary);
      word-break: break-all;

      &:first-child {
        word-break: normal;
      }
    }

    tr:nth-child(2n) {
      background-color: var(--rs-bg-well);
    }

    th {
      text-align: left;
    }
  }

  /* ===== Block Elements ===== */
  blockquote {
    padding: 0.75rem;
    margin: 1.5rem 0;
    font-size: 14px;
    border-inline-start: 5px solid var(--rs-state-warning);
    background-color: var(--rs-bg-warning);
    border-radius: 4px;
  }

  /* ===== Inline Elements ===== */
  kbd {
    box-sizing: border-box;
    font-family: var(--font-family-mono);
    border-radius: 0.25em;
    background-color: var(--rs-bg-well);
    padding: 0.2em 0.3em;
    border-style: solid;
    border-color: rgb(200, 200, 200);
    border-image: initial;
    border-width: 1px 1px 2px;
    font-size: 0.875em;
    color: var(--rs-text-primary);
  }

  // Inline code styling
  p,
  li,
  td,
  th {
    & > code:not(.rcv-highlight) {
      padding: 0.2em 0.4em;
      margin: 0;
      font-size: 85%;
      white-space: break-spaces;
      background-color: var(--rs-color-muted);
      border-radius: 6px;
      color: var(--rs-text-primary);
    }
  }

  .grid-cols-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
  }
}

/* ===== Code Blocks ===== */
code:not(.rcv-highlight) {
  font-size: 0.8rem;
  margin-inline: 2px;
  white-space: pre-wrap;
  font-family:
    Roboto Mono,
    Monaco,
    courier,
    monospace;
  color: rgb(236, 80, 96);
}

/* ===== Dark Theme Overrides ===== */
.rs-theme-dark,
.rs-theme-high-contrast {
  .rcv-markdown {
    blockquote {
      background-color: var(--rs-gray-800);
      border-color: var(--rs-yellow-500);
    }
  }
}

/* ===== Documentation Steps ===== */
.rs-doc-steps {
  counter-reset: step-counter;
  padding: 0;
  position: relative;
  display: flex;
  flex-direction: column;

  // Vertical connecting line
  &::before {
    content: '';
    position: absolute;
    inset-inline-start: 1rem;
    top: 1rem;
    bottom: 1rem;
    width: 2px;
    background-color: var(--rs-gray-100);
    transform: translateX(-50%);
    z-index: 1;
  }

  .rs-doc-step-header {
    position: relative;
    margin: 0.5rem 0;
    padding-inline-start: 2.5rem;
    font-size: 18px;
    font-weight: 600;
    color: var(--rs-text-primary);
    min-height: 2rem;
    display: flex;
    align-items: center;

    &::before {
      content: counter(step-counter);
      counter-increment: step-counter;
      position: absolute;
      inset-inline-start: 0;
      top: 50%;
      transform: translateY(-50%);
      display: flex;
      align-items: center;
      justify-content: center;
      width: 2rem;
      height: 2rem;
      background-color: var(--rs-gray-100);
      color: var(--rs-text-secondary);
      border-radius: 6px;
      font-size: 1rem;
      font-weight: bold;
      z-index: 2;
    }
  }

  .rs-doc-step-body {
    padding-inline-start: 2.5rem;
  }

  // Code blocks within steps
  pre {
    margin-top: 1rem;
    margin-bottom: 1.5rem;
    border-radius: 6px;
  }

  // Dark theme styles
  .rs-theme-dark &,
  .rs-theme-high-contrast & {
    &::before {
      background-color: var(--rs-gray-700);
    }

    .rs-doc-step-header {
      &::before {
        background-color: var(--rs-gray-700);
        color: var(--rs-text-primary);
      }
    }
  }
}

/* ===== Responsive Styles ===== */
@media (max-width: vars.$screen-md) {
  .rcv-markdown {
    table {
      display: block;
      width: 100%;
      overflow-x: auto;
      -webkit-overflow-scrolling: touch;
      margin-bottom: 16px;
      border-radius: 6px;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

      // Responsive table rows
      tr {
        display: flex;
        flex-direction: column;
        border-bottom: 1px solid var(--rs-border-primary);
        padding: 8px;
        position: relative;

        &:last-child {
          border-bottom: none;
        }

        &:nth-child(2n) {
          background-color: var(--rs-bg-well);
        }
      }

      // Hide table headers on mobile
      thead {
        display: none;
      }

      // Responsive table cells
      td,
      th {
        padding: 10px 8px;
        border: none;
        background: none;
        word-break: break-word;
        overflow-wrap: break-word;
        white-space: normal;
        position: relative;

        code {
          word-break: keep-all;
          overflow-wrap: break-word;
        }

        &:before {
          content: attr(data-label);
          font-weight: bold;
          margin-inline-end: 8px;
          min-width: 120px;
          display: none; /* Hidden by default, can be enabled with JS */
        }

        &:first-child {
          font-weight: bold;
          font-size: 1.05em;
          border-bottom: 1px dashed var(--rs-border-primary);
          margin-bottom: 4px;
          padding-bottom: 8px;
        }

        &:not(:first-child) {
          padding-inline-start: 12px;
          border-inline-start: 2px solid var(--rs-state-info);
          margin-inline-start: 4px;
        }
      }
    }

    .grid-cols-3 {
      grid-template-columns: repeat(1, 1fr);
    }
  }
}
